var db = require('../config/db')
var common = require('../common/common')
var sql = require('./dbsql/moduleSql')

/**
 * 模块服务层
 * Author: 梦里小白龙 <jiangwenshu1990@126.com> <http://www.jz07.cn>
 */

 //添加模块
const save = (value =>{
    return  new Promise(( resolve, reject ) => {
        db( sql.insert, value).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//更新模块
const update = ( params => {
    let res = [ params.pid , params.title , params.txt , params.icon , params.module , params.name , params.status , params.id ];
    return  new Promise(( resolve, reject ) => {
        db( sql.updateModule, res ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//查询模块信息
const getById = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.getById, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//模块数据
const udelete = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.deleteModule, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//根据pid查询数据
const getParentId =(pid =>{
    return  new Promise(( resolve, reject ) => {
        db( sql.getParentId, pid ).then(data=>{
            if(!data){
                reject( err )
            }else{
                resolve(data);
            }
        })
    })
})

//根据pid查询出关联树
const parentAllModule =(pid =>{
    return  new Promise(( resolve, reject ) => {
        db( sql.getParentId, pid ).then(data=>{
            if(!data){
                reject( err )
            }else{
                resolve(common.parentChildren(data));
            }
        })
    })
})


module.exports = {
    save,
    update,
    getById,
    udelete,
    getParentId,
    parentAllModule
}

